from TenCentYun import TXyun
import ujson as json

def TX_Cloud_Send(handle,topic,msg_dir):
    msg_json=json.dumps(str(msg_dir))
    handle.publish(topic, msg_json)   # 发布消息

def TX_Cloud_Init(productID,devicename,devicePsk,ProductSecret,topic,callback): 
    try:
        handle=TXyun(productID, devicename, devicePsk, ProductSecret)  # 创建连接对象
        handle.setMqtt()  # 设置mqtt
        handle.setCallback(callback)   # 设置消息回调函数
        
        handle.subscribe(topic)   # 订阅Topic
        handle.start()
        #tenxun.publish(topic, "hello world mqtt")   # 发布消息
        msg_dir = {"test0":"","test1":1}
        TX_Cloud_Send(handle,topic,msg_dir)     
        return handle     
    except Exception as e:
        print("TX_Cloud Error",e)
        return None

